﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;

namespace MockPrjTeam4.Common
{
    public class Methods
    {
        public static int ConvertNumber(string expression)
        {
            int number;
            return int.TryParse(expression, new NumberStyles(), CultureInfo.CurrentCulture, out number) ? number : 0;
        }
        /// <summary>
        /// Methods convert a string to float
        /// </summary>
        /// <param name="expression">Chuỗi convert</param>
        /// <returns>Nếu convert được thì trả về giá trị còn ko thì trả về 0</returns>
        public static float ConvertFloating(string expression)
        {
            float number;
            return float.TryParse(expression, new NumberStyles(), CultureInfo.CurrentCulture, out number) ? number : 0;
        }
        /// <summary>
        /// Methods convert a string to double
        /// </summary>
        /// <param name="expression">Chuỗi convert</param>
        /// <returns>Nếu convert được thì trả về giá trị còn ko thì trả về 0</returns>
        public static double ConvertDouble(string expression)
        {
            double number;
            return double.TryParse(expression, new NumberStyles(), CultureInfo.CurrentCulture, out number) ? number : 0;
        }
        /// <summary>
        /// Methods convert a string to string
        /// </summary>
        /// <param name="expression">Chuỗi convert</param>
        /// <returns>Nếu convert được thì trả về giá trị còn ko thì trả về chuỗi rỗng</returns>
        public static string ConvertString(string expression)
        {
            return !string.IsNullOrEmpty(expression) ? expression : "";
        }
        /// <summary>
        /// Methods convert a string to bool
        /// </summary>
        /// <param name="expression">Chuỗi convert</param>
        /// <returns>Nếu convert được thì trả về giá trị còn ko thì trả về false</returns>
        public static bool ConvertBoolean(string expression)
        {
            bool flag;
            return bool.TryParse(expression, out flag) ? flag : false;
        }
        /// <summary>
        /// Convert Date to int
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static int ConvertTryDatetimeToInt(string expression)
        {
            if (string.IsNullOrEmpty(expression))
                return -1;
            string[] time = expression.Split(':');
            if (ConvertNumber(time[0]) > 23)
                return -1;
            if (ConvertNumber(time[1]) > 59)
                return -1;
            return ConvertNumber(time[0]) * 100 + ConvertNumber(time[1]);
        }
        /// <summary>
        /// Convert Datatime to string
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static string ConvertTryDatetimeToString(string expression)
        {
            if (string.IsNullOrEmpty(expression))
                return "00000000";
            string[] time = expression.Split(':');
            if (ConvertNumber(time[0]) > 23)
                return "00000000";
            if (ConvertNumber(time[1]) > 59)
                return "00000000";
            return string.Format(CultureInfo.CurrentCulture, "{0}{1}", time[0], time[1]);
        }
        /// <summary>
        /// Methods convert a string to datetime
        /// </summary>
        /// <param name="expression">Chuỗi convert</param>
        /// <returns>Nếu convert được thì trả về giá trị còn ko thì trả về 0</returns>
        public static DateTime ConvertDateTime(string expression)
        {
            DateTime dateTime;
            return DateTime.TryParse(expression, CultureInfo.CurrentCulture, new DateTimeStyles(), out dateTime) ? dateTime : DateTime.MinValue;
        }
    }
}